package com.owen.server.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.owen.dao.InterviewDao;
import com.owen.pojo.Interview;
import com.owen.server.InterviewServer;
import com.owen.utils.ftl.GenerateDocxUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

@Service
public class InterviewServerImpl implements InterviewServer {

    @Autowired
    private InterviewDao interviewDao;

    @Override
    public PageInfo<Interview> findAll(Integer pageNum, Integer pageSize) {
        pageNum = Optional.ofNullable(pageNum).map(p -> p < 1 ? 1 : p).orElse(1);
        pageSize = Optional.ofNullable(pageSize).map(p -> p < 1 ? 10 : p).orElse(10);
        PageHelper.startPage(pageNum, pageSize);
        List<Interview> interviewAll = interviewDao.findAll();
        PageInfo<Interview> pageInfo = new PageInfo<Interview>(interviewAll);
        return pageInfo;
    }

    @Override
    public PageInfo<Interview> findByInterview(Interview interview, Integer pageNum, Integer pageSize) {
        pageNum = Optional.ofNullable(pageNum).map(p -> p < 1 ? 1 : p).orElse(1);
        pageSize = Optional.ofNullable(pageSize).map(p -> p < 1 ? 10 : p).orElse(10);
        PageHelper.startPage(pageNum, pageSize);
        List<Interview> interviewAll = interviewDao.findByInterview(interview);
        PageInfo<Interview> pageInfo = new PageInfo<Interview>(interviewAll);
        return pageInfo;
    }

    @Override
    public List<Interview> findByMedium(Interview interview) {
        List<Interview> interviewAll = interviewDao.findByInterview(interview);
        return interviewAll;
    }

    @Override
    public Interview findById(Integer id) {
        return interviewDao.findById(id);
    }

    @Override
    public Integer deleteById(Integer id) {
        return interviewDao.deleteById(id);
    }

    @Override
    public Integer deleteBatch(Integer [] ids) {
        return interviewDao.deleteBatch(ids);
    }

    @Override
    public Integer save(Interview interview) {
        return interviewDao.save(interview);
    }

    @Override
    public Integer update(Interview interview) {
        return interviewDao.update(interview);
    }

    @Override
    public void downloadDocx(Integer id, HttpServletResponse response) throws Exception {
        Interview interview = interviewDao.findById(id);
        if (interview != null) {
            Map<String, Object> dataMap = BeanUtil.beanToMap(interview, false, true);
            dataMap.forEach((k,v) -> {
                if (v instanceof Date) {
                    String dateFormat = DateUtil.format((Date) v, DatePattern.NORM_DATETIME_MINUTE_PATTERN);
                    dataMap.put(k, dateFormat);
                }
            });

            // 获取采访时间取年月日
            Date interviewTime = interview.getInterviewTime();
            // 获取年
            String year = DateUtil.format(interviewTime, "yyyy");
            // 获取月
            String month = DateUtil.format(interviewTime, "MM");
            // 获取日
            String day = DateUtil.format(interviewTime, "dd");

            dataMap.put("year", year);
            dataMap.put("month", month);
            dataMap.put("day", day);

            GenerateDocxUtil.exportDocx(dataMap, "Interview.flt", interview.getTheme(), response);
        }


    }


}
